<?php
defined( '_VALID_MOS' ) or die( '非法链接.' );
require_once('common.inc.php');
require_once(_INC_PATH.'database.php');
require_once(_INC_PATH.'page.inc.php');
include_once(_INC_PATH.'global.php' );

$id = intval($_REQUEST['id']);
if($id < 0)show404();

$smarty->assign("id",$id);
$db = new database();
$sql = "select tech.*,con.content from tbl_tech tech left join tbl_tech_content con on con.techId=tech.id where tech.id='$id' and tech.del_flg='0' and tech.published='1'";
$row = $db->loadRow($sql);
if(empty($row))show404();
$row['mTime'] = date("Y年m月d日 H:i:s",$row['mTime']);
$row['content'] = codeStyle($row['content']);
$smarty->assign("row",$row);
$smarty->assign('name',$row['title']);

//评论
$sql = "select com.* from tbl_tech_comment com left join tbl_tech tech on tech.id=com.techId where com.del_flg='0' and com.show_flg='1' and com.techId='$id'";
$db->setQuery($sql);
$commentRows = $db->loadAssocList();
foreach($commentRows as $key=>$info){
	$commentRows[$key]['create_time'] = date("Y-m-d H:i:s",$info['create_time']);
}
$smarty->assign("commentRows",$commentRows);

//热门教程
$cid = intval($row['cid']);
$jcWhere = " where published='1' and id != '$id' and cid='$cid'";
$sql = "select * from tbl_tech $jcWhere order by pv desc limit 10";
$db->setQuery($sql);
$rmRows = $db->loadAssocList();
$smarty->assign("rmRows",$rmRows);

//相关教程
$sql = "select * from tbl_tech $jcWhere order by upNum limit 10";
$db->setQuery($sql);
$xgRows = $db->loadAssocList();
$smarty->assign("xgRows",$xgRows);

//最新教程
$sql = "select * from tbl_tech $jcWhere order by mTime desc limit 10";
$db->setQuery($sql);
$zxRows = $db->loadAssocList();
$smarty->assign("zxRows",$zxRows);

//上一篇，下一篇
$sql = "select id,title from tbl_tech where del_flg='0' and published='1' and id<'$id' limit 1";
$preRow = $db->loadRow($sql);
if(is_array($preRow) && count($preRow) > 0){
	$preTech = '<a rel="pretech" href="/tech/'.$preRow['id'].'.html">'.$preRow['title'].'</a>';
}else{
	$preTech = '没有上一篇了';
}
$smarty->assign("preTech",$preTech);
$sql = "select id,title from tbl_tech where del_flg='0' and published='1' and id>'$id' limit 1";
$nextRow = $db->loadRow($sql);
if(is_array($nextRow) && count($nextRow) > 0){
	$nextTech = '<a rel="nexttech" href="/tech/'.$nextRow['id'].'.html">'.$nextRow['title'].'</a>';
}else{
	$nextTech = '没有下一篇了';
}
$smarty->assign("nextTech",$nextTech);

$pageinfo = Fetch("tech.tpl");

//$dir = _CATEGORY_PATH;
if(!is_dir($dir))cMkDir($dir);
//writeFile($pageinfo, $dir.$filename);
echo $pageinfo;die;

function codeStyle($content)
{
	preg_match_all('#\[code\](.*)\[/code\]#isU',$content,$codeMatches);
	if(is_array($codeMatches[1]) && count($codeMatches[1])>0)
	{
		include( _INC_PATH.'geshi/geshi.php' );
		foreach($codeMatches[1] as $key=>$code)
		{
			$_code = $code;
			$_code = str_replace('&lt;','<',$_code);
			$_code = str_replace('&gt;','>',$_code);
			$sourceCode = $_code;
			preg_match_all('#\[codetype\](.*)\[/codetype\]#isU',$content,$codetypeMatches);
			if(isset($codetypeMatches[1][$key]) && $codetypeMatches[1][$key] != ""){
				$codetype = $codetypeMatches[1][$key];
			}else{
				$codetype = 'php';
			}
			
			$geshi = new GeSHi($_code,$codetype);
			$geshi->set_header_type(GESHI_HEADER_DIV);
			$css = 'font-family:"Lucida Console","Courier New",Courier;'.'font-size:12px;padding-left:15px;'.'border:#BBB 1px solid;'.'background:#F7F7F7;width:650px';
			$geshi->set_overall_style($css, false);
			$geshi->set_overall_class('code');
			$geshi->enable_classes();
			$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
			$_code = '<style>.code ol{list-style:decimal;padding-left:24px;} ol li{list-style:decimal;}' . $geshi->get_stylesheet(true) . '</style>';
			$_code.= $geshi->parse_code();

			$content = str_replace($code,$_code,$content);
			$content = preg_replace('#\[copycode\]#i','<a class="copybtn" rel="'.$key.'" id="copybtn_'.$key.'">复制代码</a><div id="copycode_'.$key.'" style="display:none;">'.rawurlencode($sourceCode).'</div>',$content,1);
		}
	}
	$content = str_replace('[code]','',$content);
	$content = str_replace('[/code]','',$content);
	$content = preg_replace('#\[codetype\](.*)\[/codetype\]#isU','',$content);
	
	return $content;
}
?>